<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 组织架构组件 -->
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="company" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形组件 -->
        <el-tree :data="list" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="branch" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">
                          添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDelete(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 弹框 -->
      <el-dialog
        :title="isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <addor-edit
          :id="curId"
          ref="deptDialogRef"
          :origin-list="originList"
          :is-edit="isEdit"
          @success="success"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, deleteDepartments } from '@/api/departments'
// 导入工具函数
import { tranListToTreeData } from '@/utils'
// 引入弹框子组件
import AddorEdit from './deptDialog.vue'
export default {
  components: { AddorEdit },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 是否显示弹层
      curId: '', // 保存id
      isEdit: false, // 添加框or编辑框
      originList: [] // 原始数据
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 查询企业的部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments()
        console.log('查询企业的部门列表', res)
        this.originList = res.data.depts.map((item) => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        console.log('原始数据originList', this.originList)
        // shift会取出数组第一个元素，同时会改变原数组 -> 删除第一个元素 返回剩下的元素
        const company = res.data.depts.shift()
        console.log('删除的第一个部门', company)
        // this.list = res.data.depts
        this.list = tranListToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 添加部门
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 编辑部门
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
      // 引用子组件的方法重新获取数据
      this.$nextTick(() => {
        // 调用子组件的方法
        this.$refs.deptDialogRef.loadDepartmentsDetail()
      })
    },
    // 关闭弹窗 -> 自定义事件
    success() {
      this.showDialog = false
      this.loadDepartments()
    },
    // 删除部门
    hDelete(id) {
      //
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 确认删除
          await deleteDepartments(id)
          this.loadDepartments() // 重新渲染数据
        })
        .catch((error) => {
          // 取消删除
          console.log(error)
        })
    },
    // 表单关闭事件->引用子组件 调用子组件清空表单方法
    hClose() {
      this.$refs.deptDialogRef.resetForm()
    }
  }
}
</script>
